﻿// Autore: Alessio Parma

using System.Collections.Generic;

namespace RecipesManager.Data.Implementation
{
    sealed partial class Measure : IMeasure
    {
        #region IMeasure Members

        MeasureFamily IMeasure.Family
        {
            get { return (MeasureFamily) Family; }
        }

        string INamedEntity.Name
        {
            get { return Name; }
        }

        MeasureType IMeasure.Type
        {
            get { return (MeasureType) Type; }
        }

        public bool Equals(IMeasure other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return Id == other.Id;
        }

        #endregion

        public static Measure New(string name, string shortName, MeasureType type, MeasureFamily family,
                                  double conversionFactor)
        {
            name = name.ToKey();
            var t = (byte) type;
            var f = (byte) family;
            return CreateMeasure(0, name, shortName, t, f, conversionFactor);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return ReferenceEquals(this, obj) || Equals(obj as IMeasure);
        }

        public override int GetHashCode()
        {
            return Id;
        }

        public override string ToString()
        {
            return string.Format("Measure(Name: {0})", Name);
        }
    }

    sealed class MeasureObjects : NamedObjects<Measure, IMeasure>, IMeasures
    {
        public MeasureObjects() : base(Manager.MeasureSet)
        {
        }

        #region IMeasures Members

        public IMeasure Add(string name, string shortName, MeasureType type, MeasureFamily family,
                            double conversionFactor)
        {
            return Add(Measure.New(name, shortName, type, family, conversionFactor));
        }

        public override IEnumerator<IMeasure> GetEnumerator()
        {
            IEnumerable<IMeasure> measures = Manager.MeasureSet();
            return measures.GetEnumerator();
        }

        #endregion
    }
}